package models;

import javax.persistence.Embeddable;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

import org.codehaus.jackson.annotate.JsonIgnoreProperties;
import org.codehaus.jackson.annotate.JsonProperty;

import play.data.validation.Constraints.Required;
import play.db.ebean.Model;

@Entity
public class YouUser extends Model {

	@Id
	public long youId;
	
	@Embedded	
	public FacebookData fbdata;
	
	public static Model.Finder<Long, YouUser> find = new Finder<Long,YouUser>(Long.class,YouUser.class);
	
	public static YouUser create() {
		YouUser user = new YouUser();
		user.fbdata = FacebookData.DUMMY;
		user.save();
		return user;
	}

	public void updateFacebookData(FacebookData data) {
		this.fbdata = data;
		this.save();
	}
	
	public void clearLinkData() {
		this.fbdata = FacebookData.DUMMY;
		this.save();
	}
	
	@Embeddable
	@JsonIgnoreProperties(ignoreUnknown = true)
	public static class FacebookData {
		@Required
		@JsonProperty("id")
		public long fbid;
		
		@Required
		@JsonProperty("username")
		public String username;
		
		@Required 
		@JsonProperty("name")
		public String name;
		
		@Required
		public String accessToken;

		public static FacebookData DUMMY = create(-1,"","","");
		
		@Override
		public String toString() {
			return "FacebookData [uid=" + fbid + ", username=" + username
					+ ", name=" + name + "accessToken=" + accessToken + "]";
		}

		public static FacebookData create(long uid, String username, String name, String token) {
			FacebookData data = new FacebookData();
			data.fbid = uid;
			data.name = name;
			data.username = username;
			data.accessToken = token;
			return data;
		}
	}
}